package org.example;

public class Test3 {
    //leetcode 583 两个字符串的删除操作 https://leetcode.cn/problems/delete-operation-for-two-strings/description/
    public int minDistance(String word1, String word2) {
        //变相求两个字符串的最长公共子序列
        char[] arr1 = word1.toCharArray(), arr2 = word2.toCharArray();
        int n1 = arr1.length, n2 = arr2.length;
        int[][] dp = new int[n1 + 1][n2 + 1];
        for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= n2; j++) {
                if (arr1[i-1] == arr2[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else {
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return n1+n2 - dp[n1][n2]*2;
    }
}
